<?php

namespace App\Services;

use App\Exceptions\InternalException;
use App\Models\User;
use App\Models\UserAuth;
use Illuminate\Support\Facades\Log;

class UserAuthService
{
    public function insert($input){
        try {
            $user = UserAuth::create($input);
            return $user;
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function update($input){
        try {
            $user = UserAuth::find($input['id']);
            unset($input['id']);
            foreach ($input as $k => $v) {
                $user->$k = $v;
            }
            $user->save();
            $user = User::find($user->user_id);
            $user->tokens()->delete();
            return $user;
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }
}